package com.zz.inventory.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zz.inventory.mapper.FranchiseBillsMapper;
import com.zz.inventory.domain.FranchiseBills;
import com.zz.inventory.service.IFranchiseBillsService;

/**
 * 账单Service业务层处理
 * 
 * @author zz
 * @date 2025-07-03
 */
@Service
public class FranchiseBillsServiceImpl implements IFranchiseBillsService 
{
    @Autowired
    private FranchiseBillsMapper franchiseBillsMapper;

    /**
     * 查询账单
     * 
     * @param billId 账单主键
     * @return 账单
     */
    @Override
    public FranchiseBills selectFranchiseBillsByBillId(Long billId)
    {
        return franchiseBillsMapper.selectFranchiseBillsByBillId(billId);
    }

    /**
     * 查询账单列表
     * 
     * @param franchiseBills 账单
     * @return 账单
     */
    @Override
    public List<FranchiseBills> selectFranchiseBillsList(FranchiseBills franchiseBills)
    {
        return franchiseBillsMapper.selectFranchiseBillsList(franchiseBills);
    }

    /**
     * 新增账单
     * 
     * @param franchiseBills 账单
     * @return 结果
     */
    @Override
    public int insertFranchiseBills(FranchiseBills franchiseBills)
    {
        franchiseBills.setCreateTime(DateUtils.getNowDate());
        return franchiseBillsMapper.insertFranchiseBills(franchiseBills);
    }

    /**
     * 修改账单
     * 
     * @param franchiseBills 账单
     * @return 结果
     */
    @Override
    public int updateFranchiseBills(FranchiseBills franchiseBills)
    {
        return franchiseBillsMapper.updateFranchiseBills(franchiseBills);
    }

    /**
     * 批量删除账单
     * 
     * @param billIds 需要删除的账单主键
     * @return 结果
     */
    @Override
    public int deleteFranchiseBillsByBillIds(Long[] billIds)
    {
        return franchiseBillsMapper.deleteFranchiseBillsByBillIds(billIds);
    }

    /**
     * 删除账单信息
     * 
     * @param billId 账单主键
     * @return 结果
     */
    @Override
    public int deleteFranchiseBillsByBillId(Long billId)
    {
        return franchiseBillsMapper.deleteFranchiseBillsByBillId(billId);
    }

    @Override
    public FranchiseBills selectByOrderNo(String billNo) {
        return franchiseBillsMapper.selectFranchise(billNo);     //支付宝 查询订单信息
    }

    @Override
    public void updateById(FranchiseBills orders) {
         franchiseBillsMapper.updateFran(orders);     //支付宝 更新订单状态
    }

    @Override
    public Map<String, Object> getBillSummary(String startDate, String endDate, String status) {
        Map<String, Object> summary = new HashMap<>();

        // 总账单数
        int totalBills = franchiseBillsMapper.countBills(startDate, endDate, status);
        summary.put("totalBills", totalBills);

        // 总金额
        BigDecimal totalAmount = franchiseBillsMapper.sumAmount(startDate, endDate, status);
        summary.put("totalAmount", totalAmount != null ? totalAmount : BigDecimal.ZERO);

        // 已支付账单数
        int paidBills = franchiseBillsMapper.countBills(startDate, endDate, "已支付");
        summary.put("paidBills", paidBills);

        // 支付率
        double paymentRate = totalBills > 0 ? (paidBills * 100.0 / totalBills) : 0;
        summary.put("paymentRate", Math.round(paymentRate * 100) / 100.0);

        // 计算同比上月趋势数据（简化示例，实际应根据业务需求实现）
        summary.put("billTrend", calculateTrend("count"));
        summary.put("amountTrend", calculateTrend("amount"));
        summary.put("paidTrend", calculateTrend("paid"));
        summary.put("rateTrend", calculateTrend("rate"));

        return summary;
    }

    @Override
    public List<Map<String, Object>> getMonthlyAmountTrend(String startDate, String endDate, String status) {
        return franchiseBillsMapper.selectMonthlyAmountTrend(startDate, endDate, status);
    }

    @Override
    public List<Map<String, Object>> getBillStatusDistribution(String startDate, String endDate) {
        return franchiseBillsMapper.selectBillStatusDistribution(startDate, endDate);
    }

    @Override
    public List<Map<String, Object>> getPackageRanking(String startDate, String endDate) {
        return franchiseBillsMapper.selectPackageRanking(startDate, endDate);
    }

    @Override
    public List<Map<String, Object>> getSchoolConsumptionRanking(String startDate, String endDate) {
        return franchiseBillsMapper.selectSchoolConsumptionRanking(startDate, endDate);
    }

    // 辅助方法，计算趋势（简化版）
    private double calculateTrend(String type) {
        // 这里应该实现实际的趋势计算逻辑
        // 示例中返回随机值用于演示
        return new Random().nextInt(20) - 10; // -10到10之间的随机数
    }

}
